package cn.hxzy.icon.icon.controller;

import cn.hxzy.icon.icon.entity.Role;
import cn.hxzy.icon.icon.entity.po.RolePo;
import cn.hxzy.icon.icon.entity.vo.ReturnBean;
import cn.hxzy.icon.icon.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author little black
 */
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 查询全部
     *
     * @return
     */
    @PostMapping("list")
    public ReturnBean list() {
        List<Role> all = roleService.findAll();
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("deleteId")
    public ReturnBean delete(Integer id) {
        roleService.deleteById(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @PostMapping("add")
    public ReturnBean add(Role role, Integer roleId) {
        if (role != null) {
            role.setId(roleId);
            roleService.add(role);
            return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
        }
        return ReturnBean.of(ReturnBean.AnswerCode.ADD_ERROR);
    }

    /**
     * 根据id查角色
     *
     * @param id
     * @return
     */
    @PostMapping("findById")
    public ReturnBean findById(Integer id) {
        Role role = roleService.FindById(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, role);
    }
    @PostMapping("authorize")
    public ReturnBean authorize(Integer roleId,Integer resourceId) {
        roleService.authorize(roleId,resourceId);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }

    /**
     * 修改角色
     *
     * @param rolePo
     * @return
     */
    @PostMapping("update")
    public ReturnBean update(RolePo rolePo) {
        Role role = new Role();
        BeanUtils.copyProperties(rolePo, role);
        roleService.update(role);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }
}
